package com.my.vertx.http;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;

/**
 * 使用vertx来创建http client
 * <p>
 *     <a href="https://vertx.io/docs/vertx-core/java/#_creating_an_http_client">参考vertx - Creating an HTTP Client</a>
 * </p>
 *
 * @author: marshal
 * @date: 2023-04-12 14:02
 */
public class VertxHttpClientTest {
    // 创建vertx对象
    static Vertx vertx = Vertx.vertx();
    // 创建http client
    static HttpClient httpClient = vertx.createHttpClient();
    public static void main(String[] args) {
        // 测试http bin接口
//      testHttpBin();
        // 测试访问自己创建的http server
        testHttpServer();
    }

    /**
     * 测试请求本地运行的httpbin程序接口
     */
    private static void testHttpBin() {
        // 创建请求头
        MultiMap headers = HttpHeaders.set("content-type", "application/json").set("other-header", "foo");
        // 创建一个request
        Future<HttpClientRequest> future = httpClient.request(HttpMethod.GET, 9527, "127.0.0.1", "/get");
        future.onComplete((AsyncResult<HttpClientRequest> ar ) -> {
            if (ar.succeeded()) {
                HttpClientRequest request = ar.result();
                // 设置请求头
                request.headers().addAll(headers);
                // 发送request
                request
                        .send()
                        .onComplete((AsyncResult<HttpClientResponse> ar2)  -> {
                            if (ar2.succeeded()) {
                                // 拿到response
                                HttpClientResponse response = ar2.result();
                                System.out.println("Received response with status code : " + response.statusCode());
                                // 拿到response里面的body
                                response.body().onComplete(ar3 -> {
                                    if (ar3.succeeded()) {
                                        Buffer buffer = ar3.result();
                                        System.out.println("Received a part of the response body:");
                                        System.out.println(buffer);
                                    }
                                });
                            }
                        });
            } else {
                System.out.println("Failure: " + ar.cause().getMessage());
            }
        });
    }

    /**
     * 测试请求自己创建的http server
     * @see VertxHttpServerTest
     */
    private static void testHttpServer() {
        // 创建请求头
        MultiMap headers = HttpHeaders.set("content-type", "application/json").set("other-header", "foo");
        // 创建一个request
        Future<HttpClientRequest> future = httpClient.request(HttpMethod.GET, 9600, "127.0.0.1", "/get?name=Jack");
        future.onComplete((AsyncResult<HttpClientRequest> ar ) -> {
            if (ar.succeeded()) {
                HttpClientRequest request = ar.result();
                // 设置请求头
                request.headers().addAll(headers);
                // 发送request
                request
                        .send()
                        .onComplete(ar2 -> {
                            if (ar2.succeeded()) {
                                // 拿到response
                                HttpClientResponse response = ar2.result();
                                System.out.println("Received response with status code : " + response.statusCode());
                                // 拿到response里面的body
                                response.body().onComplete(ar3 -> {
                                    if (ar3.succeeded()) {
                                        Buffer buffer = ar3.result();
                                        System.out.println("Received content from server:");
                                        System.out.println(buffer);
                                    }
                                });
                            }
                        });
            } else {
                System.out.println("Failure: " + ar.cause().getMessage());
            }
        });
    }
}
